<template>
    <div>
        <Header/>
        <div class="m-context">

            <el-form ref="form" :model="form" label-width="100px" :rules="rules">
                <!--prop属性绑定字段名name，表单验证时，就会验证el-input元素绑定的变量-->
                <el-form-item label="标题" prop="title">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>

                <el-form-item label="摘要" prop="description">
                    <el-input type="textarea" v-model="form.description"></el-input>
                </el-form-item>

                <!--内容 markdown-->
                <el-form-item label="内容" prop="context">
                    <mavon-editor v-model="form.content"></mavon-editor>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('form')">修改</el-button>
                    <el-button @click="resetForm('form')">重置</el-button>
                </el-form-item>

            </el-form>
        </div>

    </div>
</template>

<script>

    import Header from "../components/Header";

    export default {
        name: "BlogEdit",
        data() {
            return {
                form: {
                    id: '',
                    title: '',
                    description: '',
                    content: '',
                },
                rules: {
                    title: [
                        {required: true, message: '请输入标题', trigger: 'blur'},
                        {min: 3, max: 25, message: '长度在3-25', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入摘要', trigger: 'blur'},
                    ],
                    content: [
                        {required: true, message: '请输入内容', trigger: 'blur'},
                    ],
                }
            };


        },

        created() {
            // 通过路由中的url地址栏来去当前用户id
            const blodId = this.$route.params.blogId;

           const _this = this;
            if (blodId) {
                // 回显数据到那条博客
                this.$axios.get("/blog/" + blodId).then(res => {
                    const blog = res.data.data;
                    _this.form.id = blog.id;
                    _this.form.title = blog.title;
                    _this.form.description = blog.description;
                    _this.form.content = blog.content;

                })
            }

        },

        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const _this = this;
                        this.$axios.post("/blog/edit", this.form, {
                            headers: {
                                "Authorization": localStorage.getItem("token")
                            }
                        }).then(res => {
                            // 提交新增博客成功
                            _this.$alert('操作成功', '提示', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    _this.$router.push("/blog/blogs");
                                }
                            });

                        })

                    } else {
                        console.log('error submit');
                        return false
                    }
                })
            },
            // 重置
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        },

        components: {Header},

        computed: {},
    }
</script>

<style lang="less" scoped>
    .m-context {
        margin: 0 auto;
    }

</style>
